package com.yuyou.fn.educational.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mq.RedisMessageQueue;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.ClassTime;
import com.yuyou.fn.educational.entity.Teacher;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IBaseSubjectService;
import com.yuyou.fn.educational.service.ITeacherService;
import com.yuyou.fn.educational.mapper.TeacherMapper;
import com.yuyou.fn.educational.vo.TeacherQueryVo;
import com.yuyou.fn.educational.vo.TeacherSaveOrUpdateVo;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IBusTeamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Teacher 表数据服务层接口实现类
 */
@Service
public class TeacherServiceImpl extends SuperServiceImpl<TeacherMapper, Teacher, Long> implements ITeacherService {

    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IBaseSubjectService baseSubjectService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IBusTeamService busTeamService;


    @Resource
    private RedisMessageQueue redisMessageQueue;


    @Transactional
    @Override
    public void saveOrUpdateTeacher(TeacherSaveOrUpdateVo teacherSaveOrUpdateVo) {

        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacher, teacherSaveOrUpdateVo);

        fillExtraData(teacher);

        if (teacher.getTeacherId() == null) {
            teacher.setCreateTime(TimeUtils.now());
            teacher.setUpdateTime(TimeUtils.now());
            insertSelective(teacher);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ownerId",teacher.getTeacherId());
            jsonObject.put("name",teacher.getTeacherName());
            jsonObject.put("tag","teacher");
            jsonObject.put("areaTeamId",teacher.getAreaTeamId());
            jsonObject.put("busTeamId",teacher.getBusTeamId());
            redisMessageQueue.publish("accountMainBody:create", JSONS.toBytes(jsonObject));


        } else {
            teacher.setUpdateTime(TimeUtils.now());

            updateSelectiveById(teacher);
        }
    }

    private void fillExtraData(Teacher teacher) {
        String teachGradeIds = teacher.getTeachGradeIds();
        if (teachGradeIds != null) {
            teacher.setTeachGradeNames(StringUtils.join(baseGradeService.findGradeNameByIds(Utils.toLongIds(teachGradeIds)), ","));
        } else {
            teacher.setTeachGradeNames(StringUtils.EMPTY);
        }

        String teachSubjectIds = teacher.getTeachSubjectIds();

        if (teachSubjectIds != null) {
            teacher.setTeachSubjectNames(StringUtils.join(baseSubjectService.findSubjectNameByIds(Utils.toLongIds(teachSubjectIds)), ","));
        } else {
            teacher.setTeachSubjectNames(StringUtils.EMPTY);
        }

        if (teacher.getAreaTeamId() != null) {
            teacher.setAreaTeamName(areaTeamService.findTeamNameById(teacher.getAreaTeamId()));
        } else {
            teacher.setAreaTeamName(StringUtils.EMPTY);
        }

        if (teacher.getBusTeamId() != null) {
            teacher.setBusTeamName(busTeamService.findTeamNameById(teacher.getBusTeamId()));
        } else {
            teacher.setBusTeamName(StringUtils.EMPTY);
        }

    }

    @Override
    public Page<Teacher> findTeacherByQueryForPage(TeacherQueryVo teacherQueryVo, int pageNo, int pageSize) {
        Example<Teacher> example = Teacher.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, teacherQueryVo);

        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public void deleteTeacher(Long teacherId) {
        deleteById(teacherId);
    }

    @Override
    public Page<Teacher> findTeacherListForClassArrangement(Long busTeamId, String gradeName, String subjectName , int pageNo, int pageSize){
        Example<Teacher> example = Teacher.createExample();

        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busTeamId" , busTeamId);
        criteria.andEqualTo("status" ,1 );
        if(gradeName != null ){
            criteria.andLike("teachGradeNames" ,"%" + gradeName + "%");
        }

        if(subjectName != null ){
            criteria.andLike("teachSubjectNames" , "%" + subjectName + "%") ;
        }
        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public String getNamesByIds(List<Long> teacherIds) {
        List<Teacher> teachers = this.findByIds(teacherIds);
        List<String> teacherNames = BeanUtils.toList(teachers, "teacherName");
        return StringUtils.join(teacherNames , ",");
    }


}